﻿using ERP.ERPSystem.HumanResources.Dto;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-设备管理-设备台账
    /// </summary>
    public class DeviceledgerService : ApplicationService, IDeviceledgerService
    {
        private readonly IRepository<DeviceledgerModel, int> _devicerepository;

        public DeviceledgerService(IRepository<DeviceledgerModel, int> devicerepository)
        {
            _devicerepository = devicerepository;
        }
        /// <summary>
        /// 添加设备台账
        /// </summary>
        /// <param name="deviceledgerDTO"></param>
        /// <returns></returns>
        public async Task<int> AddDeviceledger(DeviceledgerDTO deviceledgerDTO)
        {
            var deviceledger = ObjectMapper.Map<DeviceledgerDTO, DeviceledgerModel>(deviceledgerDTO);
            var i = await _devicerepository.InsertAsync(deviceledger);
            return i == null ? 0 : 1;
        }
        
        /// <summary>
        /// 更新设备台账
        /// </summary>
        /// <param name="deviceledgerDTO"></param>
        /// <returns></returns>
        public async Task<int> UpdateDeviceledger(DeviceledgerDTO deviceledgerDTO)
        {
            var existingDevice = await _devicerepository.FindAsync(deviceledgerDTO.Id);
            if (existingDevice == null)
            {
                return 0; // 设备不存在
            }
            
            // 更新设备信息
            existingDevice.DeviceCode = deviceledgerDTO.DeviceCode;
            existingDevice.DeviceName = deviceledgerDTO.DeviceName;
            existingDevice.Brand = deviceledgerDTO.Brand;
            existingDevice.Specification = deviceledgerDTO.Specification;
            existingDevice.Workshop = deviceledgerDTO.Workshop;
            existingDevice.Status = deviceledgerDTO.Status;
            // 不更新创建时间，保持原有的创建时间
            
            var result = await _devicerepository.UpdateAsync(existingDevice);
            return result == null ? 0 : 1;
        }
        
        /// <summary>
        /// 删除设备台账
        /// </summary>
        /// <param name="id">设备ID</param>
        /// <returns></returns>
        public async Task<int> DeleteDeviceledger(int id)
        {
            var existingDevice = await _devicerepository.FindAsync(id);
            if (existingDevice == null)
            {
                return 0; // 设备不存在
            }
            
            await _devicerepository.DeleteAsync(existingDevice);
            return 1; // 删除成功
        }
        
        /// <summary>
        /// 批量删除设备台账
        /// </summary>
        /// <param name="ids">设备ID列表</param>
        /// <returns></returns>
        public async Task<int> DeleteDeviceledgerBatch(List<int> ids)
        {
            if (ids == null || ids.Count == 0)
            {
                return 0; // 没有要删除的数据
            }
            
            var devices = await _devicerepository.GetListAsync(d => ids.Contains(d.Id));
            if (devices == null || devices.Count == 0)
            {
                return 0; // 没有找到要删除的设备
            }
            
            await _devicerepository.DeleteManyAsync(devices);
            return devices.Count; // 返回实际删除的数量
        }
        
        /// <summary>
        /// 设备台账列表展示
        /// </summary>
        /// <param name="code">设备编码</param>
        /// <param name="name">设备名称</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public  Task<List<DeviceledgerModel>> GetDeviceledgerModel(string? code, string? name)
        {
           var deviceledgerModels =  _devicerepository.GetQueryableAsync().Result
                .WhereIf(!string.IsNullOrEmpty(code), m => m.DeviceCode.Contains(code))
                .WhereIf(!string.IsNullOrEmpty(name), m => m.DeviceName.Contains(name))
                .ToList();
            return Task.FromResult(deviceledgerModels);
        }
    }
}
